using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace ChuanPlus.FormControl
{
    public partial class ControlAdminManageUser : DevExpress.XtraEditors.XtraUserControl
    {
        Users user;
        public ControlAdminManageUser()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (!FormValidate()) return;            
            user.Save();
            MessageBox.Show("New user is added","Message");
            xpColUser.Reload();
            //grid.RefreshDataSource();
        }
        public bool FormValidate()
        { 
            if (textUsername.Text.Trim() == "")
            {
                MessageBox.Show("Please enter username","Message");
                textUsername.Focus();
                return false;
            }
            if (textPassword.Text.Trim() == "")
            {
                MessageBox.Show("Please enter password","Message");
                textPassword.Focus();
                return false;
            }
            if (textAccName.Text.Trim() == "")
            {
                MessageBox.Show("Please enter Account name","Message");
                textAccName.Focus();
                return false;
            }
            user = new Users(textUsername.Text, textPassword.Text, textAccName.Text, checkPermission.Checked, checkActive.Checked);
            // if user duplicate, return 
            if (DAO.UserDAO.IsDuplicate(user))
            {
                MessageBox.Show("Username is duplicated", "Message");
                textUsername.Focus();
                textUsername.Select(0, textUsername.Text.Length);
                return false;
            }
            return true;
        }

        
    }
}
